package com.quickly.develop.common.crud;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.quickly.develop.common.entity.EntityResponse;
import com.quickly.develop.common.entity.PageRequest;
import com.quickly.develop.common.entity.PageResponse;
import com.quickly.develop.common.handler.PageHandler;
import com.quickly.develop.common.handler.PageHandler.CountHandler;
import com.quickly.develop.common.handler.PageHandler.ListHandler;


/**
 * @Description: 通用的基础controller
 * @Author: quyunde
 * @Date: 2020-05-10
 */
public class CrudController<T extends DataEntity, S extends CrudService<T, ?>> {

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private S crudService;

	/**
	 * 获取单条数据，主键UUID
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getByStrId")
	public EntityResponse<T> get(String id, HttpServletRequest request, HttpServletResponse response) {
		return new EntityResponse<T>(crudService.get(id)).SUCCESS();
	}

	/**
	 * 获取单条数据，主键Long自增长
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getByLongId")
	public EntityResponse<T> get(Long id, HttpServletRequest request, HttpServletResponse response) {
		return new EntityResponse<T>(crudService.get(id)).SUCCESS();
	}

	/**
	 * 获取单条数据，条件查询
	 * 
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/getOne")
	public EntityResponse<T> get(T entity, HttpServletRequest request, HttpServletResponse response) {
		return new EntityResponse<T>(crudService.get(entity)).SUCCESS();
	}

	/**
	 * 保存一条
	 * 
	 * @param entity
	 */
	@RequestMapping(value = "/save")
	public EntityResponse<T> save(T entity, HttpServletRequest request, HttpServletResponse response) {
		crudService.save(entity);
		return new EntityResponse<T>().SUCCESS();
	}

	/**
	 * 保存多条
	 * 
	 * @param entity
	 * @return
	 */

	@RequestMapping(value = "/saveList")
	public EntityResponse<T> saveList(List<T> list, HttpServletRequest request, HttpServletResponse response) {
		crudService.saveList(list);
		return new EntityResponse<T>().SUCCESS();
	}

	/**
	 * 修改一条
	 * 
	 * @param entity
	 */
	@RequestMapping(value = "/update")
	public EntityResponse<T> update(T entity, HttpServletRequest request, HttpServletResponse response) {
		crudService.update(entity);
		return new EntityResponse<T>().SUCCESS();
	}

	/**
	 * 修改多条
	 * 
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/updateList")
	public EntityResponse<T> updateList(List<T> list, HttpServletRequest request, HttpServletResponse response) {
		crudService.updateList(list);
		return new EntityResponse<T>().SUCCESS();
	}

	/**
	 * 删除一条，主键UUID
	 * 
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/deleteByStrId")
	public EntityResponse<T> delete(String id, HttpServletRequest request, HttpServletResponse response) {
		crudService.delete(id);
		return new EntityResponse<T>().SUCCESS();
	}

	/**
	 * 删除一条，主键Long自增长
	 * 
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/deleteByLongId")
	public EntityResponse<T> delete(Long id, HttpServletRequest request, HttpServletResponse response) {
		crudService.delete(id);
		return new EntityResponse<T>().SUCCESS();
	}

	/**
	 * 删除数据,条件删除
	 * 
	 * @param entity
	 */
	@RequestMapping(value = "/delete")
	public EntityResponse<T> delete(T entity, HttpServletRequest request, HttpServletResponse response) {
		crudService.delete(entity);
		return new EntityResponse<T>().SUCCESS();
	}

	/**
	 * 删除数据,条件删除
	 * 
	 * @param entity
	 */
	@RequestMapping(value = "/deleteList")
	public EntityResponse<T> deleteList(List<T> list, HttpServletRequest request, HttpServletResponse response) {
		crudService.deleteList(list);
		return new EntityResponse<T>().SUCCESS();
	}
	/**
	 * 查询分页数据
	 * 
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/queryAllByEntity")
	public PageResponse<T> queryAllByEntity(PageRequest<T> page, HttpServletRequest request,
			HttpServletResponse response) {
		logger.debug("列表查询");
		return new PageHandler<T>(new CountHandler<T>() {
			@Override
			public int count(PageRequest<T> page) {
				return crudService.countByEntity(page.getEntity());
			}
		}, new ListHandler<T>() {

			@Override
			public List<T> list(PageRequest<T> page) {
				return crudService.queryAllByEntity(page.getEntity());
			}
		}).handle(page);
	}
	
	/**
	 * 查询分页数据
	 * 
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/queryPageByEntity")
	public PageResponse<T> queryPageByEntity(PageRequest<T> page, HttpServletRequest request,
			HttpServletResponse response) {
		logger.debug("列表查询");
		return new PageHandler<T>(new CountHandler<T>() {
			@Override
			public int count(PageRequest<T> page) {
				return crudService.countByEntity(page.getEntity());
			}
		}, new ListHandler<T>() {

			@Override
			public List<T> list(PageRequest<T> page) {
				return crudService.queryPageByEntity(page.getEntity() ,page.getOffset(), page.getLimit());
			}
		}).handle(page);
	}

	/**
	 * 查询分页数据
	 * 
	 * @param PageRequest
	 *            page 分页对象
	 * @Author: quyunde
	 * @return: com.boot.security.server.page.table.PageTableResponse
	 * @date: 2019/10/28 16:03
	 */

	@RequestMapping(value = "/queryAllByMap", method = RequestMethod.GET)
	public PageResponse<T> queryAllByMap(PageRequest<T> page, HttpServletRequest request,
			HttpServletResponse response) {
		logger.debug("列表查询");
		return new PageHandler<T>(new CountHandler<T>() {
			@Override
			public int count(PageRequest<T> page) {
				return crudService.countAllByMap(page.getParams());
			}
		}, new ListHandler<T>() {

			@Override
			public List<T> list(PageRequest<T> page) {
				return crudService.queryAllByMap(page.getParams());
			}
		}).handle(page);
	}
	/**
	 * 查询分页数据
	 * 
	 * @param PageRequest
	 *            page 分页对象
	 * @Author: quyunde
	 * @return: com.boot.security.server.page.table.PageTableResponse
	 * @date: 2019/10/28 16:03
	 */

	@RequestMapping(value = "/queryPageByMap", method = RequestMethod.GET)
	public PageResponse<T> queryPageByMap(PageRequest<T> page, HttpServletRequest request,
			HttpServletResponse response) {
		logger.debug("列表查询");
		return new PageHandler<T>(new CountHandler<T>() {
			@Override
			public int count(PageRequest<T> page) {
				return crudService.countAllByMap(page.getParams());
			}
		}, new ListHandler<T>() {

			@Override
			public List<T> list(PageRequest<T> page) {
				return crudService.queryPageByMap(page.getParams(), page.getOffset(), page.getLimit());
			}
		}).handle(page);
	}

}
